﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using Microsoft.VisualBasic.FileIO;

namespace 合并页面
{
    public class ImageFile
    {
        Image m_myImage;
        string m_fullPath;  //图片全路径。
        string m_fileName;  //图片名称
        string m_rename;   //移到时的重命名
        //string m_imgFileName;//复制移动后的图片名称
        //int m_index;

        public ImageFile()
        {

        }
        public ImageFile(string fullPath)
        {
            this.m_fullPath = fullPath;

            //this.m_myImage = Image.FromFile(fullPath);
            FileStream file = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
            this.m_myImage = Image.FromStream(file, true);
            file.Close();
            string[] temp = fullPath.Split('\\');
            this.m_fileName = temp[temp.Length - 1];
        }

        public Image MyImage
        {
            get {
                return this.m_myImage;
            }
        }

        public string FullPath
        {
            get {
                return this.m_fullPath;
            }
        }

        public string FileName
        {
            get{
                return this.m_fileName;
            }
        }

        public string RenameImg
        {
            get {
                return this.m_rename;
            }
        }

        /// <summary>
        /// 将图片移动到指定文件夹
        /// </summary>
        /// <param name="list">存储图片信息</param>
        ///  <param name="imgPath">移动图片的路径</param>
        /// <param name="imgName">移动后的图片名称 如209A，209B</param>
        public static void CopyImageToFile(MyArrayList list,string imgPath,string imgName)
        {
            for (int i = 0; i < list.Count; i++)
            {
                string imgFullPath=((ImageFile)list[i]).FullPath;
                string imgCopyPath="";
                if(i<8)   //存入图片的路径格式为E:\\TAOBAO\\taobao\\1236-002\\images\\209A02.jpg
                    imgCopyPath=imgPath+"\\"+imgName+0+(i+2).ToString()+".jpg";
                else 
                    imgCopyPath=imgPath+"\\"+imgName+(i+2).ToString()+".jpg";
                File.Copy(imgFullPath, imgCopyPath, true);
            }
        }

        /// <summary>
        /// 将主图复制到编码根目录中。
        /// </summary>
        /// <param name="list"></param>
        /// <param name="imgPath"></param>
        public static void CopyZhuImgToFile(MyArrayList list, string imgPath)
        {
            for (int i = 0; i < list.Count; i++)
            {
                File.Copy(((ImageFile)list[i]).FullPath, imgPath + "\\" + ((ImageFile)list[i]).RenameImg,true);
            }
        }

        public static void DeleteImage(MyArrayList list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                FileSystem.DeleteFile(((ImageFile)list[i]).FullPath,UIOption.OnlyErrorDialogs,RecycleOption.SendToRecycleBin);
;
               // File.Delete(((ImageFile)list[i]).FullPath);
            }
        }

        /// <summary>
        /// 重命名图片文件
        /// </summary>
        /// <param name="list">传入的图片List</param>
        /// <param name="imgType">如果209 208为模块名</param>
        /// <param name="type">A，B等类型</param>
        public static void ImgRename(MyArrayList list, string imgType, string type)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (i < 8)   //存入图片的路径格式为E:\\TAOBAO\\taobao\\1236-002\\images\\209A02.jpg
                    ((ImageFile)list[i]).m_rename = imgType+type + 0 + (i + 2).ToString() + ".jpg";
                else
                    ((ImageFile)list[i]).m_rename = imgType+type + (i + 2).ToString() + ".jpg";
            }
        }

        /// <summary>
        /// 重命名五张主图 分别为11，12，13，14，15
        /// </summary>
        /// <param name="list"></param>
        public static void ImgZhuRename(MyArrayList list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                ((ImageFile)list[i]).m_rename = "1" + (i+1).ToString()+".jpg";
            }
        }

        public static byte[] GetImageByte(string filePath)
        {
            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            byte[] bytes = new byte[file.Length];
            file.Read(bytes, 0, bytes.Length);
            return bytes;
        }


    }
}
